package com.ibsolutions.epersely;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Utility {
	
	private final String format = "yyyy-MM-dd HH:mm:ss.SSS";
	private DateFormat dateFormat = new SimpleDateFormat(format);
	
	private Typeface robotoTypeFace;
	
	public String getToday() {
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	public String getFirstDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 001);
		Date date = calendar.getTime();
		return dateFormat.format(date);
	}
	
	public String getLastMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.DATE, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 001);
		Date date = calendar.getTime();
		return dateFormat.format(date);
	}
	
	public String getLastMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		Date date = calendar.getTime();
		return dateFormat.format(date);		
	}
	
	public String formatCustomDate(Date date) {
		return dateFormat.format(date);
	}
	
	public void setRobotoTypeFace(Context context, View view) {
		robotoTypeFace = Typeface.createFromAsset(context.getAssets(), "Roboto-Light.ttf");
		setFont(view, robotoTypeFace);
	}
	
	public void setFont(View view, Typeface robotoTypeface) {
		
		if(view instanceof ViewGroup) {
			for(int i = 0; i < ((ViewGroup)view).getChildCount(); i++) {
				setFont(((ViewGroup)view).getChildAt(i), robotoTypeface);
			}
		}
		else if (view instanceof TextView) {
			((TextView)view).setTypeface(robotoTypeface);
		}
	}
}
